package jp.co.piratealliance.pirateAllianceWeb.service.com.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import jp.co.piratealliance.pirateAllianceWeb.dao.coins.CoinsDao;
import jp.co.piratealliance.pirateAllianceWeb.dao.com.UserDao;
import jp.co.piratealliance.pirateAllianceWeb.dto.coins.GoldenCoinDto;
import jp.co.piratealliance.pirateAllianceWeb.dto.coins.SilverCoinDto;
import jp.co.piratealliance.pirateAllianceWeb.dto.com.CountryInfoDto;
import jp.co.piratealliance.pirateAllianceWeb.dto.com.UserInfoDto;
import jp.co.piratealliance.pirateAllianceWeb.dto.com.UserLanguageInfoDto;
import jp.co.piratealliance.pirateAllianceWeb.helper.CommonHelper;
import jp.co.piratealliance.pirateAllianceWeb.service.com.UserService;

/**
*
* copyright   pirate alliance team 2014
* @author     lunxing
*
*/

public class UserServiceImpl implements UserService {

	private UserDao userDao;

	private CoinsDao coinsDao;

	public UserInfoDto getUserInfo(String email, String password) {
		// TODO Auto-generated method stub
		Map<String,Object> paramMap = new HashMap<String,Object>();
		paramMap.put("email", email);
		paramMap.put("password", password);
		return userDao.selectUserByEmailAndPassword(paramMap);
	}

	public UserInfoDto userRegister(UserInfoDto userInfoDto,UserLanguageInfoDto userLanguageInfo) {

		int count = userDao.userRegister(userInfoDto);

		GoldenCoinDto goldenCoin = new GoldenCoinDto();
		goldenCoin.setGolden_coin_id(userInfoDto.getGolden_coin_id());
		goldenCoin.setAccountbalance(CommonHelper.getGoldenCoinsForReg());
		coinsDao.insertGoldenCoin(goldenCoin);

		SilverCoinDto silverCoin = new SilverCoinDto();
		silverCoin.setSilver_coin_id(userInfoDto.getSilver_coin_id());
		silverCoin.setAccountbalance(CommonHelper.getSilverCoinsForReg());
		coinsDao.insertSilverCoin(silverCoin);

		if(userLanguageInfo != null && StringUtils.isNotEmpty(userLanguageInfo.getLanguage_id()))
		{
			userLanguageInfo.setUser_id(userInfoDto.getUser_id());
			userDao.insertUserLanguage(userLanguageInfo);
		}

		if(count == 1)
			return userInfoDto;
		else
			return null;
	}

	public UserInfoDto getServiceUserInfo(String user_id) {
		// TODO Auto-generated method stub
		return userDao.selectUserByUserId(user_id);
	}

	public int updateUserInfo(UserInfoDto userInfo, UserLanguageInfoDto userLanguageInfo)
	{
		if(userLanguageInfo != null && StringUtils.isNotEmpty(userLanguageInfo.getLanguage_id()))
		{
			userLanguageInfo.setUser_id(userInfo.getUser_id());
			int ret = userDao.updateUserLanguage(userLanguageInfo);
			if(ret == 0)
			{
				userDao.insertUserLanguage(userLanguageInfo);
			}
		}
		return userDao.userUpdate(userInfo);
	}

	public List<UserInfoDto> getUserListInfo(String user_id){
		return userDao.selectUserListInfoByUserId(user_id);
	}

	public boolean checkEmailMult(String email){
		boolean succssFlag=true;
		int count =userDao.getEmailCount(email);
		if(count >0){
			succssFlag=false;
		}
		return succssFlag;
	}

	public CountryInfoDto getServiceCountryInfo(String country_id) {
		return userDao.selectCountryByCountryId(country_id);
	}

	public UserLanguageInfoDto selectUserLanguageByUserIdAndLanguageId(String user_id, String language_id) {
		// TODO Auto-generated method stub
		Map<String,Object> paramMap = new HashMap<String,Object>();
		paramMap.put("user_id", user_id);
		paramMap.put("language_id", language_id);
		return userDao.selectUserLanguageByUserIdAndLanguageId(paramMap);
	}

	public int insertUserLanguage(UserLanguageInfoDto userLanguageInfoDto)
	{
		return userDao.insertUserLanguage(userLanguageInfoDto);
	}

	public int updateUserLanguage(UserLanguageInfoDto userLanguageInfoDto)
	{
		return userDao.updateUserLanguage(userLanguageInfoDto);
	}

	/**
	 * @return the userDao
	 */
	public UserDao getUserDao() {
		return userDao;
	}

	/**
	 * @param userDao the userDao to set
	 */
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public CoinsDao getCoinsDao()
	{
		return coinsDao;
	}

	public void setCoinsDao(CoinsDao coinsDao)
	{
		this.coinsDao = coinsDao;
	}






}
